package com.oyjd.ybx.fw.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.sys.util.SysUtil;

/**
 * 会话拦截器
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class AuthorizationInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6338933709326212120L;
	private static Log log = LogFactory.getLog(AuthorizationInterceptor.class);

	/**
	 * 无限制链接
	 */
	private static List<String> freeLink = new ArrayList<String>();
	/**
	 * 前端链接
	 */
	private static List<String> frontLink = new ArrayList<String>();
	static {
		freeLink.add("sys!goLogin.action");
		freeLink.add("sys!login.action");
		freeLink.add("sys!logout.action");

		frontLink.add("front!");
		frontLink.add("frontSys!");
		frontLink.add("frontCmt!");
		frontLink.add("favorite!");
		frontLink.add("frontTalk!");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result = null;
		printPara(invocation);
		try {
			HttpServletRequest req = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
			String reqUrl = req.getRequestURI().substring(1);
			reqUrl = reqUrl.substring(reqUrl.indexOf("/") + 1);
			String p = req.getQueryString();
			if (null != p) {
				reqUrl += "?" + p;
			}

			// 验证登录和权限链接
			Map<String, Object> session = invocation.getInvocationContext().getSession();
			for (String link : freeLink) {
				if (reqUrl.startsWith(link)) {
					log.info("无限制链接！");
					return invocation.invoke();
				}
			}

			// 前端页面
			for (String link : frontLink) {
				if (reqUrl.startsWith(link)) {
					log.info("前端页面请求无限制！");
					return invocation.invoke();
				}
			}

			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			if (null == userVO) {
				log.info("没登入或用户会话已经失效！");
			} else {
				List<String> userLinks = SysUtil.roleToLinks.get(userVO.getRoleId());
				boolean flag = false;
				for (String link : userLinks) {
					if (reqUrl.startsWith(link)) {
						req.setAttribute(CodeUtil.OpKey, SysUtil.roleToModuleOp.get(userVO.getRoleId()));
						result = invocation.invoke();
						flag = true;
						log.info("角色权限链接！");
						break;
					}
				}
				if (!flag) {
					log.info("无权限链接！");
				}
			}

		} catch (Exception e) {
			log.error("会话拦截器异常！" + e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 开发参数查看
	 * 
	 * @param invocation
	 */
	@SuppressWarnings("rawtypes")
	private void printPara(ActionInvocation invocation) {
		try {
			HttpServletRequest req = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
			String reqUrl = req.getRequestURL().toString();
			StringBuffer sb = new StringBuffer("\r\n[").append(req.getRemoteHost()).append("]request-->").append(reqUrl);
			Map map = req.getParameterMap();
			for (Iterator item = map.keySet().iterator(); item.hasNext();) {
				String key = (String) item.next();
				for (String v : (String[]) map.get(key)) {
					sb.append("\r\n------para->").append(key).append("=").append(v);
				}
			}
			log.info(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
